package com.bto.solar.work.service;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.dao.IBannerService;
import com.bto.solar.work.entity.Banner;
import com.bto.solar.work.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王小波
 * @description:
 * @date 2023/9/12 11:09
 */
@Service
public class BtoBannerService {
    @Autowired
    private IBannerService bannerService;

    public void addBanner(Banner banner){
        String type = banner.getType();
        long count = bannerService.count(bannerService.lambdaQuery().eq(Banner::getType, type).getWrapper());
        if(DBConstants.BANNER_INDEX.equals(type)){
            if(count >= 1){
                throw new BizException("启动页已有，只能添加一个");
            }
        }
        else if(DBConstants.BANNER_HOME.equals(type)){
            if(count >= 4){
                throw new BizException("首页最多之能有4个");
            }
        }
        boolean save = bannerService.save(banner);
        if(!save){
            throw new BizException("新增失败");
        }
    }

    public void updateBanner(Banner banner){
        if(NullUtils.isNull(banner.getId())){
            throw new BizException("id 为空，更新失败");
        }

        boolean updated = bannerService.updateById(banner);
        if(!updated){
            throw new BizException("更新失败");
        }
    }

    public void deleteBanner(Integer bannerId){
        boolean remove = bannerService.removeById(bannerId);
        if(!remove){
            throw new BizException("删除失败");
        }
    }

    public List<Banner> getBanner(String type){
       return  bannerService.lambdaQuery()
                .eq(Banner::getType, type).list();
    }



}
